<template>
  <div class="navbar-container" v-loading.fullscreen.lock="fullscreenLoading">
    <el-popover placement="right" :width="180" popper-class="profile-popover" :show-arrow="false" trigger="click">
      <template #reference>
        <div class="logo-wrap">
          <img width="40" src="@/assets/img/navbar_logo.svg" />
        </div>
      </template>
      <div class="profile-wrap">
        <div class="userinfo">
          <div class="image"><img width="40" src="@/assets/img/navbar_logo.svg" /></div>
          <div class="username">李钟意</div>
        </div>
        <ul class="operations">
          <li class="operation-item">
            <span class="operation-label">隐私协议</span><span class="operation-icon"><IconProtocol /></span>
          </li>
          <li class="operation-item" @click="onLogout">
            <span class="operation-label">退出登录</span><span class="operation-icon"><IconLogout /></span>
          </li>
        </ul>
      </div>
    </el-popover>
  </div>
  <el-dialog v-model="logoutVisibile" title="退出登录" width="440">
    <span>您确定退出登录吗</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="onCancel" style="width: 60px">取消</el-button>
        <el-button type="primary" @click="onOK" style="width: 60px">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import IconLogout from '@/components/SvgIcon/IconLogout.vue';
import IconProtocol from '@/components/SvgIcon/IconProtocol.vue';
import {ref} from 'vue';
import {ElMessage} from 'element-plus';
import {useRouter} from 'vue-router';
import {ROUTE_LOGIN} from '@/router/names';
import {useUserStore} from '@/stores/userStore';

const router = useRouter();
const userStore = useUserStore();

const logoutVisibile = ref<boolean>(false);
const fullscreenLoading = ref<boolean>(false);

const onCancel = () => {
  logoutVisibile.value = false;
};

const onOK = async () => {
  // 1. 调用后端退出接口
  // 2. 清理本地缓存数据 - token - userinfo
  // 3. 跳转到登录页面
  try {
    fullscreenLoading.value = true;
    await new Promise((resolve) => setTimeout(resolve, 3000));
    // localStorage.removeItem('isAuthorized');

    userStore.setAuthorized('');

    logoutVisibile.value = false;

    router.push({name: ROUTE_LOGIN});
  } catch (error) {
    ElMessage({type: 'error', message: '退出登录失败，请重试'});
  } finally {
    fullscreenLoading.value = false;
  }
};

const onLogout = () => {
  logoutVisibile.value = true;
};
</script>

<style lang="scss">
.profile-popover {
  inset: 2px auto auto 74px !important;
}
</style>

<style lang="scss" scoped>
.navbar-container {
  height: 100vh;
  flex: 0 0 70px;
  background-color: #0c193d;
}

.logo-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 24px;
  img {
    width: 40px;
    height: 40px;
  }
}

.profile-wrap {
}
.userinfo {
  display: flex;
  flex-flow: row nowrap;
  align-items: center;

  height: 60px;
  border-radius: 8px 8px 0px 0px;
  background: #409eff;
}
.image {
  flex: 0 0 40px;
  height: 40px;
  margin-left: 14px;
  margin-right: 20px;
}
.username {
  flex: 1;
  color: #fff;
  font-size: 14px;
  // 名称很长需要出现...
}

.operations {
  padding-bottom: 20px;
}

.operation-item {
  display: flex;
  align-items: center;

  margin-top: 10px;
  height: 34px;
  line-height: 34px;
  padding-left: 14px;

  color: #303133;
  font-size: 14px;
  line-height: 20px;
  transition: all 0.2s ease;

  user-select: none;

  cursor: pointer;
  &:hover {
    background: #f5f5f5;
  }

  .operation-label {
    margin-right: auto;
  }

  .operation-icon {
    margin-right: 10px;
  }
}
</style>
